package com.example.demo.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;

@RestController
@RequestMapping("/admin")
public class Controller {
   private static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";
   private static final String KAPTCHA_SESSION_DATE = "KAPTCHA_SESSION_DATE";
   private static final long TIME_OUT = 60*1000;
   public boolean check(String inputCaptcha, HttpSession session){
       if(!StringUtils.hasLength(inputCaptcha)){
           return false;
       }
       String saveCaptcha = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
       Date saveCaptchaDATE =(Date) session.getAttribute(KAPTCHA_SESSION_DATE);
       if(inputCaptcha.equalsIgnoreCase(saveCaptcha)){
           if(saveCaptcha!=null&&System.currentTimeMillis()-saveCaptchaDATE.getTime()<TIME_OUT){
               return true;
           }
       }
       return false;
   }
}
